<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 反射机制深度解析</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border: 1px solid rgba(102, 126, 234, 0.3);
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 8px;
            margin-top: -8px;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 16px;
            color: white;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        code {
            background: rgba(102, 126, 234, 0.1);
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Consolas', 'Monaco', monospace;
            color: #667eea;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
            border-left: 4px solid #667eea;
            padding: 20px;
            margin: 20px 0;
            border-radius: 8px;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-6">Java 反射机制</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">探索 Java 运行时的元编程魔法</p>
            <div class="flex justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-code mr-2"></i>动态编程
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-cogs mr-2"></i>框架基石
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full text-sm">
                    <i class="fas fa-magic mr-2"></i>运行时魔法
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">反</span>射（Reflection）是 Java 语言中的一种强大机制，它允许在运行时动态地获取类的信息（如类名、方法、字段等），并且能够动态地调用方法、访问和修改字段、创建对象实例等。反射是 Java 提供的一种元编程能力，能够使程序在运行时获取类型信息，并在不知道类的情况下操作这些类的成员。
            </p>
        </div>

        <!-- Concept Map -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="text-3xl font-bold mb-6 text-gradient">反射机制概念图</h2>
            <div class="mermaid">
                graph TD
                    A[Java 反射机制] --> B[Class 对象]
                    A --> C[核心操作]
                    A --> D[应用场景]
                    
                    B --> B1[Class.forName]
                    B --> B2[类名.class]
                    B --> B3[对象.getClass]
                    
                    C --> C1[获取构造器]
                    C --> C2[获取方法]
                    C --> C3[获取字段]
                    C --> C4[获取注解]
                    
                    D --> D1[动态加载]
                    D --> D2[框架设计]
                    D --> D3[动态代理]
                    D --> D4[依赖注入]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style C fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
                    style D fill:#9f7aea,stroke:#fff,stroke-width:2px,color:#fff
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">核心概念</h2>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-cube"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">类加载与 Class 对象</h3>
                    <p class="text-gray-600 mb-3">在 Java 中，每个类都有一个与之对应的 <code>Class</code> 对象，该对象包含了类的所有信息。通过反射，我们可以在运行时获取这些信息。</p>
                    <p class="text-gray-600">使用 <code>Class<?></code> 类型对象，可以在运行时获取类的构造器、方法、字段、注解等信息。</p>
                </div>
                
                <div class="bg-white rounded-xl shadow-lg p-6 card-hover">
                    <div class="feature-icon">
                        <i class="fas fa-tools"></i>
                    </div>
                    <h3 class="text-xl font-bold mb-3">反射的基本操作</h3>
                    <ul class="space-y-2 text-gray-600">
                        <li><i class="fas fa-check-circle text-purple-500 mr-2"></i><strong>获取类对象：</strong>通过 <code>Class.forName()</code>、<code>类名.class</code> 或 <code>对象.getClass()</code></li>
                        <li><i class="fas fa-check-circle text-purple-500 mr-2"></i><strong>获取构造器：</strong>使用 <code>getConstructor()</code> 或 <code>getDeclaredConstructor()</code></li>
                        <li><i class="fas fa-check-circle text-purple-500 mr-2"></i><strong>获取方法：</strong>使用 <code>getMethod()</code> 或 <code>getDeclaredMethod()</code></li>
                        <li><i class="fas fa-check-circle text-purple-500 mr-2"></i><strong>获取字段：</strong>使用 <code>getField()</code> 或 <code>getDeclaredField()</code></li>
                    </ul>
                </div>
            </div>
        </div>

        <!-- Application Scenarios -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">应用场景</h2>
            
            <div class="grid md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-indigo-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-download text-3xl text-purple-600 mb-4"></i>
                    <h4 class="font-bold mb-2">动态加载类</h4>
                    <p class="text-sm text-gray-600">反射允许在运行时加载并使用类，这对于框架设计尤为重要。</p>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-purple-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-eraser text-3xl text-blue-600 mb-4"></i>
                    <h4 class="font-bold mb-2">泛型类型检查</h4>
                    <p class="text-sm text-gray-600">在泛型擦除后，反射可以在运行时获取类型的真实信息。</p>
                </div>
                
                <div class="bg-gradient-to-br from-indigo-50 to-purple-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-layer-group text-3xl text-indigo-600 mb-4"></i>
                    <h4 class="font-bold mb-2">框架设计</h4>
                    <p class="text-sm text-gray-600">Spring、Hibernate 等框架广泛使用反射实现核心功能。</p>
                </div>
                
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 rounded-xl p-6 card-hover">
                    <i class="fas fa-random text-3xl text-purple-600 mb-4"></i>
                    <h4 class="font-bold mb-2">动态代理</h4>
                    <p class="text-sm text-gray-600">反射是动态代理的基础，支持方法拦截和增强。</p>
                </div>
            </div>
        </div>

        <!-- Pros and Cons -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-8 section-title">优缺点分析</h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-green-50 rounded-xl p-6">
                    <h3 class="text-xl font-bold mb-4 text-green-700">
                        <i class="fas fa-thumbs-up mr-2"></i>优点
                    </h3>
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold">动态性</h4>
                                <p class="text-gray-600">反射使得程序具有高度的动态性，能够在运行时处理未知的类和对象。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold">灵活性</h4>
                                <p class="text-gray-600">允许开发者编写更为灵活、通用的代码，尤其适合框架和库的开发。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="bg-red-50 rounded-xl p-6">
                    <h3 class="text-xl font-bold mb-4 text-red-700">
                        <i class="fas fa-thumbs-down mr-2"></i>缺点
                    </h3>
                    <div class="space-y-3">
                        <div class="flex items-start">
                            <i class="fas fa-times-circle text-red-500 mt-1 mr-3"></i>
                            <div>
                                <h4 class="font-semibold">性能开销</h4>
                                <p class="text-gray-600">反射是动态解析的，比直接调用代码要慢，频繁使用可能导致性能下降。</p>
                            </div>
                        </div>